{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.preprocessing import LabelEncoder\n",
    "from sklearn.preprocessing import OneHotEncoder,Binarizer\n",
    "from sklearn.model_selection import train_test_split\n",
    "import numpy as np\n",
    "from sklearn.linear_model import LogisticRegression,LinearRegression\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "from sklearn.tree import DecisionTreeClassifier\n",
    "from sklearn.metrics import accuracy_score,confusion_matrix,mean_squared_error,recall_score,roc_auc_score\n",
    "from sklearn.model_selection import GridSearchCV\n",
    "import joblib\n",
    "import matplotlib.pyplot as plt\n",
    "from sklearn.metrics import classification_report\n",
    "\n",
    "from sklearn.metrics import RocCurveDisplay\n",
    "from sklearn import metrics\n",
    "from sklearn.metrics import precision_recall_curve"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "outputs": [
    {
     "data": {
      "text/plain": "       t1     t2  label\n0   0.697  0.460      1\n1   0.774  0.376      1\n2   0.634  0.264      1\n3   0.608  0.318      1\n4   0.556  0.215      1\n5   0.403  0.237      1\n6   0.481  0.149      1\n7   0.437  0.211      1\n8   0.666  0.091      0\n9   0.243  0.267      0\n10  0.245  0.057      0\n11  0.343  0.099      0\n12  0.639  0.161      0\n13  0.657  0.198      0\n14  0.360  0.370      0\n15  0.593  0.042      0\n16  0.719  0.103      0",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>t1</th>\n      <th>t2</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>0.697</td>\n      <td>0.460</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.774</td>\n      <td>0.376</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.634</td>\n      <td>0.264</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.608</td>\n      <td>0.318</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.556</td>\n      <td>0.215</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>0.403</td>\n      <td>0.237</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>0.481</td>\n      <td>0.149</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>0.437</td>\n      <td>0.211</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>8</th>\n      <td>0.666</td>\n      <td>0.091</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>0.243</td>\n      <td>0.267</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>0.245</td>\n      <td>0.057</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>0.343</td>\n      <td>0.099</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>12</th>\n      <td>0.639</td>\n      <td>0.161</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>13</th>\n      <td>0.657</td>\n      <td>0.198</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>14</th>\n      <td>0.360</td>\n      <td>0.370</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>0.593</td>\n      <td>0.042</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>16</th>\n      <td>0.719</td>\n      <td>0.103</td>\n      <td>0</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = pd.read_csv(\"C:\\\\Users\\\\Administrator\\\\Desktop\\\\月考练习算法题 (2)\\\\月考练习算法题\\\\第3套（修改2）\\\\专高6月考-03附件\\\\smt.txt\",names=[\"t1\",\"t2\",\"label\"])\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "outputs": [
    {
     "data": {
      "text/plain": "       t1     t2  label\n8   0.666  0.091      0\n1   0.774  0.376      1\n9   0.243  0.267      0\n14  0.360  0.370      0\n6   0.481  0.149      1\n16  0.719  0.103      0\n3   0.608  0.318      1\n15  0.593  0.042      0\n4   0.556  0.215      1\n0   0.697  0.460      1\n13  0.657  0.198      0\n10  0.245  0.057      0\n11  0.343  0.099      0\n5   0.403  0.237      1\n7   0.437  0.211      1\n12  0.639  0.161      0\n2   0.634  0.264      1",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>t1</th>\n      <th>t2</th>\n      <th>label</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>8</th>\n      <td>0.666</td>\n      <td>0.091</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>1</th>\n      <td>0.774</td>\n      <td>0.376</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>0.243</td>\n      <td>0.267</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>14</th>\n      <td>0.360</td>\n      <td>0.370</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>0.481</td>\n      <td>0.149</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>16</th>\n      <td>0.719</td>\n      <td>0.103</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.608</td>\n      <td>0.318</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>0.593</td>\n      <td>0.042</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.556</td>\n      <td>0.215</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>0</th>\n      <td>0.697</td>\n      <td>0.460</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>13</th>\n      <td>0.657</td>\n      <td>0.198</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>10</th>\n      <td>0.245</td>\n      <td>0.057</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>0.343</td>\n      <td>0.099</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>0.403</td>\n      <td>0.237</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>0.437</td>\n      <td>0.211</td>\n      <td>1</td>\n    </tr>\n    <tr>\n      <th>12</th>\n      <td>0.639</td>\n      <td>0.161</td>\n      <td>0</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.634</td>\n      <td>0.264</td>\n      <td>1</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "df = df.sample(frac = 1)\n",
    "df"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "outputs": [
    {
     "data": {
      "text/plain": "       t1     t2\n10  0.245  0.057\n7   0.437  0.211\n4   0.556  0.215\n5   0.403  0.237\n9   0.243  0.267\n0   0.697  0.460\n2   0.634  0.264\n6   0.481  0.149\n15  0.593  0.042\n13  0.657  0.198\n11  0.343  0.099\n14  0.360  0.370\n3   0.608  0.318",
      "text/html": "<div>\n<style scoped>\n    .dataframe tbody tr th:only-of-type {\n        vertical-align: middle;\n    }\n\n    .dataframe tbody tr th {\n        vertical-align: top;\n    }\n\n    .dataframe thead th {\n        text-align: right;\n    }\n</style>\n<table border=\"1\" class=\"dataframe\">\n  <thead>\n    <tr style=\"text-align: right;\">\n      <th></th>\n      <th>t1</th>\n      <th>t2</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>10</th>\n      <td>0.245</td>\n      <td>0.057</td>\n    </tr>\n    <tr>\n      <th>7</th>\n      <td>0.437</td>\n      <td>0.211</td>\n    </tr>\n    <tr>\n      <th>4</th>\n      <td>0.556</td>\n      <td>0.215</td>\n    </tr>\n    <tr>\n      <th>5</th>\n      <td>0.403</td>\n      <td>0.237</td>\n    </tr>\n    <tr>\n      <th>9</th>\n      <td>0.243</td>\n      <td>0.267</td>\n    </tr>\n    <tr>\n      <th>0</th>\n      <td>0.697</td>\n      <td>0.460</td>\n    </tr>\n    <tr>\n      <th>2</th>\n      <td>0.634</td>\n      <td>0.264</td>\n    </tr>\n    <tr>\n      <th>6</th>\n      <td>0.481</td>\n      <td>0.149</td>\n    </tr>\n    <tr>\n      <th>15</th>\n      <td>0.593</td>\n      <td>0.042</td>\n    </tr>\n    <tr>\n      <th>13</th>\n      <td>0.657</td>\n      <td>0.198</td>\n    </tr>\n    <tr>\n      <th>11</th>\n      <td>0.343</td>\n      <td>0.099</td>\n    </tr>\n    <tr>\n      <th>14</th>\n      <td>0.360</td>\n      <td>0.370</td>\n    </tr>\n    <tr>\n      <th>3</th>\n      <td>0.608</td>\n      <td>0.318</td>\n    </tr>\n  </tbody>\n</table>\n</div>"
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X = df.iloc[:,0:-1]\n",
    "y = df[\"label\"]\n",
    "X_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2,random_state=42,shuffle=True)\n",
    "X_train\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "outputs": [],
   "source": [
    "class MyDecisionTree:\n",
    "    def __init__(self,splitter='best',max_depth=None,min_samples_split=2,min_samples_leaf=1):\n",
    "        self.splitter = splitter\n",
    "        self.max_depth = max_depth\n",
    "        self.min_samples_split = min_samples_split\n",
    "        self.min_samples_leaf = min_samples_leaf\n",
    "\n",
    "    def predict_proba(self, X, check_input=True):\n",
    "        X = self._validate_X_predict(X, check_input)\n",
    "        proba = self.tree_.predict(X)\n",
    "\n",
    "        if self.n_outputs_ == 1:\n",
    "            proba = proba[:, : self.n_classes_]\n",
    "            normalizer = proba.sum(axis=1)[:, np.newaxis]\n",
    "            normalizer[normalizer == 0.0] = 1.0\n",
    "            proba /= normalizer\n",
    "\n",
    "            return proba\n",
    "\n",
    "        else:\n",
    "            all_proba = []\n",
    "            for k in range(self.n_outputs_):\n",
    "                proba_k = proba[:, k, : self.n_classes_[k]]\n",
    "                normalizer = proba_k.sum(axis=1)[:, np.newaxis]\n",
    "                normalizer[normalizer == 0.0] = 1.0\n",
    "                proba_k /= normalizer\n",
    "                all_proba.append(proba_k)\n",
    "\n",
    "            return all_proba\n",
    "\n",
    "    def fit(self,x,y):\n",
    "        self.model = DecisionTreeClassifier(splitter=self.splitter,max_depth=self.max_depth,min_samples_split=self.min_samples_split,min_samples_leaf=self.min_samples_leaf)\n",
    "        self.model.fit(x,y)\n",
    "\n",
    "    def pred(self,x):\n",
    "        return self.model.predict(x)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "outputs": [],
   "source": [
    "my_tree = MyDecisionTree()\n",
    "my_tree.fit(X_train,y_train)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "outputs": [
    {
     "data": {
      "text/plain": "1.0"
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "y_pred = my_tree.pred(X_test)\n",
    "accuracy_score(y_test,y_pred)"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [],
   "source": [
    "#todo\n",
    "# 创建网格以绘制分界线\n",
    "x_min, x_max = X.iloc[:, 0].min() - 1, X.iloc[:, 0].max() + 1\n",
    "y_min, y_max = X.iloc[:, 1].min() - 1, X.iloc[:, 1].max() + 1"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "outputs": [],
   "source": [
    "xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.1),\n",
    "                     np.arange(y_min, y_max, 0.1))\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "F:\\python38\\lib\\site-packages\\sklearn\\base.py:465: UserWarning: X does not have valid feature names, but DecisionTreeClassifier was fitted with feature names\n",
      "  warnings.warn(\n"
     ]
    }
   ],
   "source": [
    "Z = my_tree.pred(np.c_[xx.ravel(), yy.ravel()])\n",
    "Z = Z.reshape(xx.shape)\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "outputs": [
    {
     "data": {
      "text/plain": "<Figure size 640x480 with 1 Axes>",
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAggAAAGKCAYAAABpbLktAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90bGliIHZlcnNpb24zLjUuMSwgaHR0cHM6Ly9tYXRwbG90bGliLm9yZy/YYfK9AAAACXBIWXMAAA9hAAAPYQGoP6dpAAAgp0lEQVR4nO3deXCU953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Dnyu+bW7wKOjqWtTa3yuNr7lhni0ce6+WfPR55Wi7/3OqRx9Pxc5vXw989MIh99vf72ff49VhCN3vFdVy4cEGZmZndeSsAAAiz0tJSZWRkXHd5twMhGAyqrKxMbrdbFoul2wMCAID+EwqF1NTUpLS0NFmt1z/ToNuBAAAAbl2cpAgAAAwEAgAAMBAIAADAQCAAAAADgQAAAAwEAgAAMBAIAADAQCAAAAADgQAAAAwEAgAAMBAIAADAQCAAAADD/wcL8foW/Xxm+QAAAABJRU5ErkJggg=="
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.scatter(X.iloc[:, 0], X.iloc[:, 1], c=y, cmap=plt.cm.Paired, edgecolors='k')\n",
    "\n",
    "# 绘制分界线\n",
    "plt.contourf(xx, yy, Z, alpha=0.4)\n",
    "plt.xlim(xx.min(), xx.max())\n",
    "plt.ylim(yy.min(), yy.max())\n",
    "plt.xticks(())\n",
    "plt.yticks(())\n",
    "plt.show()"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "outputs": [],
   "source": [
    "from matplotlib.colors import ListedColormap\n",
    "def plot_decision_region(X,y,classifier,resolution=0.02):\n",
    "    markers = ('s','x','o','^','v')\n",
    "    colors = ('red','blue','lightgreen','gray','cyan')\n",
    "    # 背景色\n",
    "    cmap = ListedColormap(colors[:len(np.unique(y))])\n",
    "\n",
    "    # plot the decision surface\n",
    "    #这里+1  -1的操作我理解为防止样本落在图的边缘处，不知道对不对\n",
    "    x1_min,x1_max = X[:,0].min()-1,X[:,0].max()+1\n",
    "    #print(x1_min, x1_max)\n",
    "\n",
    "    x2_min,x2_max = X[:,1].min()-1,X[:,1].max()+1\n",
    "    #print(x2_min, x2_max)\n",
    "\n",
    "    # 生成网格点坐标矩阵\n",
    "    xx1,xx2 = np.meshgrid(np.arange(x1_min,x1_max,resolution),\n",
    "                         np.arange(x2_min,x2_max,resolution))\n",
    "    Z = classifier.predict(np.array([xx1.ravel(),xx2.ravel()]).T)\n",
    "    Z = Z.reshape(xx1.shape)\n",
    "    # 绘制轮廓等高线  alpha参数为透明度\n",
    "    plt.contourf(xx1,xx2,Z,alpha=0.3,cmap=cmap)\n",
    "    plt.xlim(xx1.min(),xx1.max())\n",
    "    plt.ylim(xx2.min(),xx2.max())\n",
    "\n",
    "    # plot class samples\n",
    "    for idx,cl in enumerate(np.unique(y)):\n",
    "        plt.scatter(x=X[y==cl,0],\n",
    "                   y = X[y==cl,1],\n",
    "                   alpha=0.8,\n",
    "                   c=colors[idx],\n",
    "                   marker = markers[idx],\n",
    "                   label=cl,\n",
    "                   edgecolors='black')\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "ename": "InvalidIndexError",
     "evalue": "(slice(None, None, None), 0)",
     "output_type": "error",
     "traceback": [
      "\u001B[1;31m---------------------------------------------------------------------------\u001B[0m",
      "\u001B[1;31mTypeError\u001B[0m                                 Traceback (most recent call last)",
      "File \u001B[1;32mF:\\python38\\lib\\site-packages\\pandas\\core\\indexes\\base.py:3621\u001B[0m, in \u001B[0;36mIndex.get_loc\u001B[1;34m(self, key, method, tolerance)\u001B[0m\n\u001B[0;32m   3620\u001B[0m \u001B[38;5;28;01mtry\u001B[39;00m:\n\u001B[1;32m-> 3621\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_engine\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mget_loc\u001B[49m\u001B[43m(\u001B[49m\u001B[43mcasted_key\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m   3622\u001B[0m \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mKeyError\u001B[39;00m \u001B[38;5;28;01mas\u001B[39;00m err:\n",
      "File \u001B[1;32mF:\\python38\\lib\\site-packages\\pandas\\_libs\\index.pyx:136\u001B[0m, in \u001B[0;36mpandas._libs.index.IndexEngine.get_loc\u001B[1;34m()\u001B[0m\n",
      "File \u001B[1;32mF:\\python38\\lib\\site-packages\\pandas\\_libs\\index.pyx:142\u001B[0m, in \u001B[0;36mpandas._libs.index.IndexEngine.get_loc\u001B[1;34m()\u001B[0m\n",
      "\u001B[1;31mTypeError\u001B[0m: '(slice(None, None, None), 0)' is an invalid key",
      "\nDuring handling of the above exception, another exception occurred:\n",
      "\u001B[1;31mInvalidIndexError\u001B[0m                         Traceback (most recent call last)",
      "Cell \u001B[1;32mIn[28], line 1\u001B[0m\n\u001B[1;32m----> 1\u001B[0m \u001B[43mplot_decision_region\u001B[49m\u001B[43m(\u001B[49m\u001B[43mX_train\u001B[49m\u001B[43m,\u001B[49m\u001B[43my_train\u001B[49m\u001B[43m,\u001B[49m\u001B[43mclassifier\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[43mmy_tree\u001B[49m\u001B[43m,\u001B[49m\u001B[43mresolution\u001B[49m\u001B[38;5;241;43m=\u001B[39;49m\u001B[38;5;241;43m0.01\u001B[39;49m\u001B[43m)\u001B[49m\n\u001B[0;32m      2\u001B[0m plt\u001B[38;5;241m.\u001B[39mxlabel(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mpetal length\u001B[39m\u001B[38;5;124m'\u001B[39m)\n\u001B[0;32m      3\u001B[0m plt\u001B[38;5;241m.\u001B[39mylabel(\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mpetal width\u001B[39m\u001B[38;5;124m'\u001B[39m)\n",
      "Cell \u001B[1;32mIn[27], line 10\u001B[0m, in \u001B[0;36mplot_decision_region\u001B[1;34m(X, y, classifier, resolution)\u001B[0m\n\u001B[0;32m      6\u001B[0m cmap \u001B[38;5;241m=\u001B[39m ListedColormap(colors[:\u001B[38;5;28mlen\u001B[39m(np\u001B[38;5;241m.\u001B[39munique(y))])\n\u001B[0;32m      8\u001B[0m \u001B[38;5;66;03m# plot the decision surface\u001B[39;00m\n\u001B[0;32m      9\u001B[0m \u001B[38;5;66;03m#这里+1  -1的操作我理解为防止样本落在图的边缘处，不知道对不对\u001B[39;00m\n\u001B[1;32m---> 10\u001B[0m x1_min,x1_max \u001B[38;5;241m=\u001B[39m \u001B[43mX\u001B[49m\u001B[43m[\u001B[49m\u001B[43m:\u001B[49m\u001B[43m,\u001B[49m\u001B[38;5;241;43m0\u001B[39;49m\u001B[43m]\u001B[49m\u001B[38;5;241m.\u001B[39mmin()\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m,X[:,\u001B[38;5;241m0\u001B[39m]\u001B[38;5;241m.\u001B[39mmax()\u001B[38;5;241m+\u001B[39m\u001B[38;5;241m1\u001B[39m\n\u001B[0;32m     11\u001B[0m \u001B[38;5;66;03m#print(x1_min, x1_max)\u001B[39;00m\n\u001B[0;32m     13\u001B[0m x2_min,x2_max \u001B[38;5;241m=\u001B[39m X[:,\u001B[38;5;241m1\u001B[39m]\u001B[38;5;241m.\u001B[39mmin()\u001B[38;5;241m-\u001B[39m\u001B[38;5;241m1\u001B[39m,X[:,\u001B[38;5;241m1\u001B[39m]\u001B[38;5;241m.\u001B[39mmax()\u001B[38;5;241m+\u001B[39m\u001B[38;5;241m1\u001B[39m\n",
      "File \u001B[1;32mF:\\python38\\lib\\site-packages\\pandas\\core\\frame.py:3505\u001B[0m, in \u001B[0;36mDataFrame.__getitem__\u001B[1;34m(self, key)\u001B[0m\n\u001B[0;32m   3503\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39mcolumns\u001B[38;5;241m.\u001B[39mnlevels \u001B[38;5;241m>\u001B[39m \u001B[38;5;241m1\u001B[39m:\n\u001B[0;32m   3504\u001B[0m     \u001B[38;5;28;01mreturn\u001B[39;00m \u001B[38;5;28mself\u001B[39m\u001B[38;5;241m.\u001B[39m_getitem_multilevel(key)\n\u001B[1;32m-> 3505\u001B[0m indexer \u001B[38;5;241m=\u001B[39m \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mcolumns\u001B[49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43mget_loc\u001B[49m\u001B[43m(\u001B[49m\u001B[43mkey\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m   3506\u001B[0m \u001B[38;5;28;01mif\u001B[39;00m is_integer(indexer):\n\u001B[0;32m   3507\u001B[0m     indexer \u001B[38;5;241m=\u001B[39m [indexer]\n",
      "File \u001B[1;32mF:\\python38\\lib\\site-packages\\pandas\\core\\indexes\\base.py:3628\u001B[0m, in \u001B[0;36mIndex.get_loc\u001B[1;34m(self, key, method, tolerance)\u001B[0m\n\u001B[0;32m   3623\u001B[0m         \u001B[38;5;28;01mraise\u001B[39;00m \u001B[38;5;167;01mKeyError\u001B[39;00m(key) \u001B[38;5;28;01mfrom\u001B[39;00m \u001B[38;5;21;01merr\u001B[39;00m\n\u001B[0;32m   3624\u001B[0m     \u001B[38;5;28;01mexcept\u001B[39;00m \u001B[38;5;167;01mTypeError\u001B[39;00m:\n\u001B[0;32m   3625\u001B[0m         \u001B[38;5;66;03m# If we have a listlike key, _check_indexing_error will raise\u001B[39;00m\n\u001B[0;32m   3626\u001B[0m         \u001B[38;5;66;03m#  InvalidIndexError. Otherwise we fall through and re-raise\u001B[39;00m\n\u001B[0;32m   3627\u001B[0m         \u001B[38;5;66;03m#  the TypeError.\u001B[39;00m\n\u001B[1;32m-> 3628\u001B[0m         \u001B[38;5;28;43mself\u001B[39;49m\u001B[38;5;241;43m.\u001B[39;49m\u001B[43m_check_indexing_error\u001B[49m\u001B[43m(\u001B[49m\u001B[43mkey\u001B[49m\u001B[43m)\u001B[49m\n\u001B[0;32m   3629\u001B[0m         \u001B[38;5;28;01mraise\u001B[39;00m\n\u001B[0;32m   3631\u001B[0m \u001B[38;5;66;03m# GH#42269\u001B[39;00m\n",
      "File \u001B[1;32mF:\\python38\\lib\\site-packages\\pandas\\core\\indexes\\base.py:5637\u001B[0m, in \u001B[0;36mIndex._check_indexing_error\u001B[1;34m(self, key)\u001B[0m\n\u001B[0;32m   5633\u001B[0m \u001B[38;5;28;01mdef\u001B[39;00m \u001B[38;5;21m_check_indexing_error\u001B[39m(\u001B[38;5;28mself\u001B[39m, key):\n\u001B[0;32m   5634\u001B[0m     \u001B[38;5;28;01mif\u001B[39;00m \u001B[38;5;129;01mnot\u001B[39;00m is_scalar(key):\n\u001B[0;32m   5635\u001B[0m         \u001B[38;5;66;03m# if key is not a scalar, directly raise an error (the code below\u001B[39;00m\n\u001B[0;32m   5636\u001B[0m         \u001B[38;5;66;03m# would convert to numpy arrays and raise later any way) - GH29926\u001B[39;00m\n\u001B[1;32m-> 5637\u001B[0m         \u001B[38;5;28;01mraise\u001B[39;00m InvalidIndexError(key)\n",
      "\u001B[1;31mInvalidIndexError\u001B[0m: (slice(None, None, None), 0)"
     ]
    }
   ],
   "source": [
    "plot_decision_region(X_train,y_train,classifier=my_tree,resolution=0.01)\n",
    "plt.xlabel('petal length')\n",
    "plt.ylabel('petal width')\n",
    "plt.legend(loc='upper left')\n",
    "plt.show()\n"
   ],
   "metadata": {
    "collapsed": false,
    "pycharm": {
     "name": "#%%\n"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}